package com.py.credit.service.impl;

import com.py.credit.dao.AuthorizeModelDao;
import com.py.credit.dao.AuthorizeRecordDao;
import com.py.credit.entity.AuthorizeModel;
import com.py.credit.entity.AuthorizeRecord;
import com.py.credit.service.AuthorizeModelService;
import com.py.framework.core.common.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * @author YaoS
 * @date 2019-05-13 14:04
 */
@Service
public class AuthorizeModelServiceImpl implements AuthorizeModelService {

    @Autowired
    AuthorizeModelDao modelDao;

    @Autowired
    AuthorizeRecordDao recordDao;

    @Override
    public List findModelList() {
        return modelDao.findModelList();
    }

    @Override
    public void addAuthorizeModel(AuthorizeModel model) {
        modelDao.addAuthorizeModel(model);
    }

    @Override
    public List<AuthorizeModel> selectModelListForPage(String modelName, String authorizeName, Page page) {
        return modelDao.selectModelListForPage(modelName, authorizeName, page);
    }

    @Override
    public List<AuthorizeRecord> findModelBind(String modelId) {
        return recordDao.findModelBind(modelId);
    }

    @Override
    public AuthorizeModel selectModelDetail(String authorizeId) {
        return modelDao.selectModelDetail(authorizeId);
    }


    @Override
    public AuthorizeModel selectModel(String modelId) {
        return modelDao.selectModel(modelId);
    }

    @Override
    public void updateAuthorizeModel(AuthorizeModel model) {
        modelDao.updateAuthorizeModel(model);
    }

    @Override
    public void deleteAuthorizeModelById(Integer modelId) {
        modelDao.deleteAuthorizeModelById(modelId);
    }

    @Override
    public int selectCount(String modelId) {
        return modelDao.selectCount(modelId);
    }


    @Override
    public void deleteBind(String modelId) {
        modelDao.deleteBind(modelId);
    }

    @Override
    public void modelBind(String modelId, List<String> recordIds) {
        modelDao.deleteBind(modelId);
        if (recordIds != null && !recordIds.isEmpty()) {
            modelDao.modelBind(modelId, recordIds);
        }
    }

    @Override
    public HashMap<String, Integer> selectAuthorize(String token, String modelId) {
        return modelDao.selectAuthorize(token, modelId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void run(List<String> ids) {
        recordDao.deleteRecordByIds(ids);
        List<String> tokens = recordDao.selectNotExistDeptToken();
        recordDao.deleteAssociation(tokens);
    }

}
